#include<AT89X52.h>
#define uchar unsigned char
unsigned char NeiRong[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x00,0x00,0x00,0x00};  
uchar Buffer[6]={0x00,0x00,0x00,0x00,0x00,0x80}; 
uchar MiMa[6]={0,1,2,3,4,5}; 
uchar MiMaBiaoZhi;
uchar count=0,AnJian=-1,x=0,k=0,Y=9,E=9,S=0;  
uchar c;  


void delay(unsigned char n)  
     {
         uchar a,b;
         for(a=n;a>0;a++) 
		 {
		   for(b=n;b>0;b++)
            {
		    } 
         }
     } 


void fengmiqi()
   {
        P3=0x0;     
        delay(100);
        P3=0xFF;    
   }


void Time0Int(void)
      {
           TH0=0x0f0; 
           TL0=0x05f;
           TMOD=0x01;   
           TCON=0x10;   
           IE=0x82;   
      }



void KeyScan()
    {
       uchar x,y;
       P2=0xf0;    
       if(P2==0xf0)
         {
             c=0;    
         }
       else
         {     
             switch(P2)   
              {
                 case 0x70:x=0;break;
                 case 0xb0:x=1;break;
                 case 0xd0:x=2;break;
                 case 0xe0:x=3;break;
              }
                 P2=0x0f;
             switch(P2)  
              {
                 case 0x07:y=3;break;
                 case 0x0b:y=2;break;
                 case 0x0d:y=1;break;
                 case 0x0e:y=0;break;
              }
            AnJian=4*x+y;         
		 	c=1;            
			delay(100);    
            }	 
 }



void Time1Int(void) interrupt 1
     {  
        count++;
        if(AnJian!=-1 && c==0)   
	      {  	
	       	if(x<6)
	      	{
              Buffer[x]=NeiRong[AnJian];    
		      if(NeiRong[MiMa[x]]==Buffer[x])
		     {
                k++;                         
             }
	        } 	 
	     AnJian=-1;   
         x++;
	      }
	    if(count==2)   
	             {
	                   P1=0x7f;
	                   P0=Buffer[5];
	             }
		if(count==3) 
	             {
	                   P1=0xbf;
	                   P0=Buffer[4];	 
	             }
	     if(count==4)
	             {
	                   P1=0xdf;
	                   P0=Buffer[3];	 
	             }
	 	 if(count==5) 
	             {
	                   P1=0xef;
	                   P0=Buffer[2];	 
	             }
         if(count==6) 
	            {
	                   P1=0xf7;
	                   P0=Buffer[1];	 
	            }
	 	 if(count==7)
	             {
	                   P1=0xfb;
	                   P0=Buffer[0];	 
             	 }
	     if(count==8) 
	             {  
        
			      if( MiMaBiaoZhi==0)    
	                {
			          S++;
			        }

	              if(S==130)   
	                {
                      Y--;
	                  S=0; 
					}             
	                  P1=0xfd;
        	          P0=NeiRong[Y];   
	              
	              if(Y==-1)
	               {
	                 E--;
	                 Y=9;
	               } 
			    }
	 
        if(count==9)
	           {
	              P1=0xfe;
	              P0=NeiRong[E];
	              count=0;
	           }
       if(E==-1)   
           {
	        while(1)
	         {
	           fengmiqi();        
	           delay(100); 
     		   IE=0x80;
	         }
           }
         TH0=0xfa;   
         TL0=0xff;
}		 



void main()
{

  Time0Int();
  while(1)
   {      
	  KeyScan();
 	  if(AnJian==13)     
     	 {
		      Buffer[0]=Buffer[1]=Buffer[2]=Buffer[3]=Buffer[4]=0x00;     
	          Buffer[5]=0x80;                                 
			  AnJian=-1;                                         
			  break;		
     	 }


      if(AnJian==14&&c==1)  
	     {  
		    fengmiqi();     
		    { 	 
              Buffer[x-1]=0x00;  
	          x--;              
			  AnJian=-1;	     
	          delay(1);
		    }
		 }


     if(x>5)
	    {
		  if(AnJian==12&&c==1)    
		   {
		     fengmiqi();        
		     if(k!=6)
	          {
                Buffer[0]=Buffer[1]=Buffer[2]=Buffer[3]=Buffer[4]=Buffer[5]=0x3f;
			    MiMaBiaoZhi=0;         
                fengmiqi();             
				delay(100);
				fengmiqi();            
  			    delay(100);
			    fengmiqi();               
  			    delay(100);
				fengmiqi();            
  			    delay(100);
				fengmiqi();            
  			    delay(100);	
			    fengmiqi();             
  			    delay(100);	
	          }
		     else
	           {  
		         	Buffer[0]=Buffer[1]=Buffer[2]=Buffer[3]=Buffer[4]=Buffer[5]=0x06;	 		
			        MiMaBiaoZhi=1;          					 	  
               }             
		   }
	     
	    }
    }
}
